import pygame, math, sys, random, time
from player import Player
from penguin import Penguin
from eskimo import Eskimo
from backgrounds import Background
from fence import Fence
from bullet import Bullet

if pygame.mixer:
    pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)

penguins = pygame.sprite.Group()
eskimos = pygame.sprite.Group()
fences = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all = pygame.sprite.OrderedUpdates()

Player.containers = all
Penguin.containers = all, penguins
Eskimo.containers = all, eskimos
Background.containers = all
Fence.containers = all, fences
Bullet.containers = all, bullets

backgroundList = []
backgroundList += [Background([800, 600], screenSize, "rsc\\images\\backgrounds\\backround3.png")]
backgroundList += [Background([0, 600], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([1600, 600], screenSize, "rsc\\images\\backgrounds\\tRoadFoo.png")]
backgroundList += [Background([1600, 0], screenSize, "rsc\\images\\backgrounds\\verticalroad1.png")]
backgroundList += [Background([1600, -600 ], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([1600, 1200 ], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([800, 1200 ], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([2400, 600], screenSize, "rsc\\images\\backgrounds\\culdesac.png")]
backgroundList += [Background([0, 1200], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([-800, 1200], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([-800, 600], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([-800, 0], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([0, 0], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([800, 100 ], screenSize, "rsc\\images\\backgrounds\\green.png")]
backgroundList += [Background([2400, 0 ], screenSize, "rsc\\images\\backgrounds\\green.png")]

buildingsList = []
buildingsList += [Background([1900, 350], screenSize, "rsc\\images\\germanhouse.png")]
buildingsList += [Background([1100, 300], screenSize, "rsc\\images\\allianz.png")]

eastereggsList = []
eastereggsList
eastereggsList

fenceList = []
fenceList += [Fence([0,900], screenSize, "rsc\\images\\fence\\fence.png")]
fenceList += [Fence([-400, 600], screenSize, "rsc\\images\\fence\\upfence.png")]
fenceList += [Fence([0, 300], screenSize, "rsc\\images\\fence\\fence.png")]

bulletList = []

player = Player([2,2], [400, 300], screenSize, "rsc//images//Redneck//Redneck.png")


penguinList = []
maxEnemies = 5

backgroundColor = 0,0,0

startTime = time.clock()

run = True
run2 = False

#--------------------------Keybinding---------------------------------    
while True:
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.direction("left")
                elif event.key == pygame.K_RIGHT:
                    player.direction("right")
                elif event.key == pygame.K_UP:
                    player.direction("up")
                elif event.key == pygame.K_DOWN:
                    player.direction("down")
                elif event.key == pygame.K_q:
                    bulletList += [Bullet("rsc\\images\\Bullet\\bullet.png",
                                            [34, 0], 
                                            [player.realx, player.realy], 
                                            player.dir, 
                                            screenSize,
                                            [player.scrollingX, player.scrollingY],
                                            [player.x, player.y])]
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player.direction("stop")
                elif event.key == pygame.K_RIGHT:
                    player.direction("stop")
                elif event.key == pygame.K_UP:
                    player.direction("stop")
                elif event.key == pygame.K_DOWN:
                    player.direction("stop")
            
        
        #Create Enemies
        while len(penguins) < maxEnemies:
            penguinSpeed = [5,4]
            penguinPos = [random.randint(100,screenWidth-100),
                       random.randint(100,screenHeight/2)]
            penguinList += [Penguin(penguinSpeed, penguinPos, screenSize, "rsc\images\Penguin\PenguinL.png", 2)]
            
    #    print "input, make penquin", time.clock() - startTime
        
        playerHitPenguins = pygame.sprite.spritecollide(player, penguins, False)
        playerHitFences = pygame.sprite.spritecollide(player, fences, False)
        
        penguinsHitFences = pygame.sprite.groupcollide(penguins, fences, False, False)
        
        for enemy in playerHitPenguins:
            player.enemyCollide(enemy)
            
        for fence in playerHitFences:
            player.fenceCollide(fence)
            
        for penguin in penguinsHitFences:
            for fence in penguinsHitFences[penguin]:
                penguin.fenceCollide(fence)
        if player.living == False:
            run2 = True
            run = False
    #    print "collide", time.clock() - startTime
                
        all.update(player.realx, 
                   player.realy, 
                   player.scrollingX,
                   player.scrollingY)
        
        #Draw Everything to Screen
        
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        clock.tick(30)
    #    print "draw", time.clock() - startTime
        #print clock.get_fps()
        
    while run2:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            deathScreen = pygame.image.load("rsc\\images\\dead\\deathscreen.png")
            deathrect = deathScreen.get_rect()
            screen.blit(deathScreen, deathrect)
            pygame.display.flip()
            clock.tick(30)
